package com.sds.sweng.swat.miplatform.service.pages;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Node;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;

import com.sds.sweng.swat.miplatform.service.figures.components.SelectableComponent;
import com.sds.sweng.swat.miplatform.service.figures.components.ValidationComponent;
import com.sds.sweng.swat.miplatform.service.validation.ValidationInfo;
import com.sds.sweng.swat.miplatform.service.validation.ValidationProperties;
import com.sds.sweng.swat.miplatform.service.viewers.ServiceDatasetViewer;
import com.sds.sweng.swat.miplatform.service.viewers.ServiceDetailViewer;
import com.sds.sweng.swat.miplatform.service.viewers.ValidationViewer;

public class ServiceControlArea extends Composite {

	private MiplatformServicePage page;

	private ServiceDatasetViewer serviceDatasetViewer;
	private ServiceDetailViewer serviceDetailViewer;
	private ValidationViewer validationViewer;

	private ExpandItem datasetItem;
	private ExpandItem serviceItem;
	private ExpandItem validationItem;

	private Node currentService;
	private String currentServiceId;

	private SelectableComponent selectedComponent;

	public ServiceControlArea(MiplatformServicePage parent) {
		super(parent, SWT.NONE);
		this.page = parent;

		initArea();
	}

	private void initArea() {
		setLayout(new FillLayout());

		ExpandBar expandBar = new ExpandBar(this, SWT.V_SCROLL);

		// Services
		datasetItem = new ExpandItem(expandBar, SWT.NONE);
		datasetItem.setText("Services");

		serviceDatasetViewer = new ServiceDatasetViewer(expandBar);
		serviceDatasetViewer.setArea(this);
		datasetItem.setControl(serviceDatasetViewer);
		datasetItem.setHeight(serviceDatasetViewer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		datasetItem.setExpanded(true);

		// Service Detail
		serviceItem = new ExpandItem(expandBar, SWT.NONE);
		serviceItem.setText("Service Detail");

		serviceDetailViewer = new ServiceDetailViewer(expandBar);
		serviceItem.setControl(serviceDetailViewer);
		serviceItem.setHeight(serviceDetailViewer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		serviceItem.setExpanded(true);

		// Validation
		validationItem = new ExpandItem(expandBar, SWT.NONE);
		validationItem.setText("Validation");

		validationViewer = new ValidationViewer(expandBar, ValidationProperties.KR);
		validationViewer.setArea(this);
		validationItem.setControl(validationViewer);
		validationItem.setHeight(validationViewer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		validationItem.setExpanded(true);
	}

	public void bind(Node dsService) {
		List<Node> serviceRecords = serviceRecords = dsService.selectNodes("Contents/record");

		serviceDatasetViewer.setInput(serviceRecords);
	}

	public void setServiceDetail(Node selectedService) {
		serviceDetailViewer.setInput(selectedService);
		serviceItem.setHeight(serviceDetailViewer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);

		if (!selectedService.equals(currentService)) {
			serviceItem.setExpanded(true);
		}

		currentService = selectedService;
		currentServiceId = selectedService.selectSingleNode("svcId").getText();

		page.setPartiedComponents(selectedService, currentServiceId);
		setSelectedComponent(selectedComponent);
		serviceDetailViewer.refresh();
	}

	public void setSelectedComponent(SelectableComponent component) {
		if (component == null) {
			return;
		}

		selectedComponent = component;

		validationViewer.setInput((ValidationComponent) component, currentServiceId);
		validationItem.setHeight(validationViewer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		validationItem.setExpanded(true);
	}

	public void setServiceValidationString() {
		List<ValidationComponent> validationComponents = page.getPartiedValidationComponents(currentServiceId);
		StringBuilder serviceValidationStringBuilder = new StringBuilder();

		for (int i=0;i<validationComponents.size();i++) {
			if (i > 0) {
				serviceValidationStringBuilder.append(ValidationInfo.validationSeparator);
			}
			serviceValidationStringBuilder.append(validationComponents.get(i).getValidationInfo(currentServiceId).toString());
		}

		String orginalServiceValidationString = currentService.selectSingleNode("validation").getText();

		String serviceValidationString = serviceValidationStringBuilder.toString();
		serviceValidationString = StringUtils.remove(serviceValidationString, "\n");
		serviceValidationString = StringUtils.remove(serviceValidationString, "\r");

		if (!serviceValidationString.equals(orginalServiceValidationString)) {
			currentService.selectSingleNode("validation").setText(serviceValidationString);
			setServiceDetail(currentService);

			page.replaceSource();
		}
	}

}
